/**=========================================================
 * Controller: TrustUserCenterController
  =========================================================*/
tzadmin.controller('TrustUserCenterController', ['$scope', 'Utils', 'HttpUtils', 'Datepicker', 'SimpleUiGrid', 'ngDialog', 'toaster', 'SimpleSelect', 'DialogUtils',
    function($scope, utils, httpUtils, datepickerService, simpleUiGridService, ngDialog, toaster, simpleSelectService, dialogUtils) {
        var createDate = datepickerService.init($scope,{initValue: ''});

        var trustUserGridColumns = [
            {
                displayName: '注册时间',
                field: 'create_date',
                width: '150'
            },
            {
                displayName: '注册账号',
                field: 'mobile',
                width: '100'
            },
            {
                displayName: '绑定邮箱',
                field: 'mail',
                width: '150'
            },
            {
                displayName: '企业名称',
                field: 'company_name'
            },
            {
                displayName: '所属商务',
                field: 'salesman',
                width: '80'
            },
            {
                displayName: '账户余额',
                field: 'fund',
                width: '80',
                cellClass: "align-right"
            },
            {
                displayName: 'API欠费',
                field: 'arrears',
                width: '80'
            },
            {
                displayName: '状态',
                field: 'frozen_status_name',
                width: '80'
            },
            { 
                name: '操作', 
                field: 'widgets', 
                width: '150',
                enableCellEdit: false,
                cellTemplate: '\
                    <div class="ui-grid-cell-contents" style="text-align:center;">\
                        <button ng-click="grid.appScope.editTrustUserDetail(row.entity)" title="编辑" class="btn btn-xs btn-success">\
                            <em class="fa fa-edit"></em>\
                        </button>\
                        <button ng-click="grid.appScope.editTrustUserFee(row.entity)" title="报价" class="btn btn-xs btn-warning">\
                            <em class="fa fa-rmb"></em>\
                        </button>\
                        <button ng-if="row.entity.frozen_status == 1" ng-click="grid.appScope.unfrozen(row.entity)" title="激活" class="btn btn-xs btn-info">\
                            <em class="fa fa-unlock"></em>\
                        </button>\
                        <button ng-if="row.entity.frozen_status == 0" ng-click="grid.appScope.frozen(row.entity)" title="冻结" class="btn btn-xs btn-info">\
                            <em class="fa fa-lock"></em>\
                        </button>\
                    </div>\
                '
            }
        ];
        
        $scope.queryParams = {namespace: 'AccountMapper.queryTrustUser'};
        $scope.searchTrustUserGrid = function() {
            $scope.queryParams.createDate = utils.dateFormat(createDate.getDate(),'yyyy-MM-dd');
            trustUserGrid.draw($scope.queryParams);
        }; 

        var trustUserGrid = simpleUiGridService.init($scope, 
            {
                optionModel: 'trustUserGridOptions',
                pageOptionModel: 'trustUserGridPageOptions',
                gridApi: 'trustUserGridApi',
                columns: trustUserGridColumns,
                pageType: 'common',
                needExport: false,
                remoteSort: true,
                controllerSearchFn: $scope.searchTrustUserGrid
            }
        );
//====================================编辑===========================================
        var salesmanSelect = simpleSelectService.init($scope,
            {
                optionModel: 'salesman',
                params: {namespace: 'AccountMapper.getSalesman'}
            }
        );

        $scope.editTrustUserDetail = function(data){
            ngDialog.openConfirm({
                template: 'editTrustUserDetailDialog',
                className: 'ngdialog-theme-default',
                appendClassName: 'ngdialog-lg',
                scope: $scope,
                controller: ['$scope', function($scope) {
                    $scope.getTrustUserDetail(data.sid);
                }]
            }).then(function (value) {
                $scope.trustUserDetail.salesman = salesmanSelect.getSelected();
                httpUtils.post('/trustUser/saveDetail', $scope.trustUserDetail,
                    function(result){
                        if (result.success){
                            toaster.pop('success', '', '保存成功');
                        }else{
                            toaster.pop('error', '', result.msg);
                        }
                    },
                    function(result){
                        toaster.pop('error', '', '保存失败');
                    }
                );
            });
        }

        $scope.getTrustUserDetail = function(sid){
            $scope.trustUserDetail = null;
            httpUtils.queryList(
                {
                    namespace:'AccountMapper.getTrustUserDetail',
                    sid: sid
                },
                function(result){
                    if (result.success){
                        $scope.trustUserDetail = result.datas.gridDatas[0];
                        $scope.trustUserDetail.imgSrc = "data:image/" + $scope.trustUserDetail.file_type + ";base64," + $scope.trustUserDetail.content;
                    }else{
                        $scope.trustUserDetail = null;
                    }
                },
                function(result){
                    toaster.pop('error', '', '获取用户详情');
                }
            );
        }
//====================================报价===========================================
        $scope.editTrustUserFee = function(data){
            ngDialog.openConfirm({
                template: 'editTrustUserFeeDialog',
                className: 'ngdialog-theme-default',
                appendClassName: 'ngdialog-lg',
                scope: $scope,
                controller: ['$scope', function($scope) {
                    $scope.getTrustUserFee(data.sid);
                }]
            }).then(function (value) {
                httpUtils.post('/trustUserApiFee/save', $scope.trustUserFee,
                    function(result){
                        if (result.success){
                            toaster.pop('success', '', '保存成功');
                        }else{
                            toaster.pop('error', '', result.msg);
                        }
                    },
                    function(result){
                        toaster.pop('error', '', '保存失败');
                    }
                );
            });
        }

        $scope.getTrustUserFee = function(sid){
            $scope.trustUserFee = null;
            httpUtils.queryList(
                {
                    namespace:'AccountMapper.getTrustUserFee',
                    sid: sid
                },
                function(result){
                    if (result.success){
                        $scope.trustUserFee = result.datas.gridDatas[0];
                    }
                },
                function(result){
                    toaster.pop('error', '', '获取用户计费失败');
                }
            );
        }
//====================================冻结===========================================
        $scope.frozen = function(data){
            dialogUtils.confirm('冻结后，该账号将不可登录探知数据官网，且其对应的API授权信息将失效。<br>确认冻结该账号吗？',
                function (value) {
                    httpUtils.post('/trustUser/frozen',data,
                        function(result){
                            if (result.success){
                                data.frozen_status = 1;
                                data.frozen_status_name = '冻结';
                                toaster.pop('success', '', '冻结成功');
                            }else{
                                toaster.pop('error', '', result.msg);
                            }
                        },
                        function(result){
                            toaster.pop('error', '', '冻结失败');
                        }
                    );
                }
            );
        };

        $scope.unfrozen = function(data){
            dialogUtils.confirm('激活后，该账号将可以登录探知数据官网，且其对应的API授权信息将激活。<br>确认激活该账号吗？',
                function (value) {
                    httpUtils.post('/trustUser/unfrozen',data,
                        function(result){
                            if (result.success){
                                data.frozen_status = 0;
                                data.frozen_status_name = '未冻结';
                                toaster.pop('success', '', '激活成功');
                            }else{
                                toaster.pop('error', '', result.msg);
                            }
                        },
                        function(result){
                            toaster.pop('error', '', '激活失败');
                        }
                    );
                }
            );
        };

        $scope.searchTrustUserGrid();
    }
]);
